#include "Paire.hpp"

#include <string.h>
#include <iostream>
#include <mpi.h>

const int SIZE_PACKAGE = 500;

using namespace std;


ostream & operator << (ostream & o, const Paire & inPaire)
{
   o << "(" << inPaire._paire[0] << ", " << inPaire._paire[1] << ")";
   return o;
}

//On n'utilise pas les fonctions MPI_Pack on met un @ entre les chaines
char * Paire::converMPI() const
{
   char * elem1 = mpz_get_str(NULL, 10, _paire[0].get_mpz_t()) ;
   char * elem2 = mpz_get_str(NULL, 10, _paire[1].get_mpz_t());
   int    size1 = strlen(elem1);
   
   char  * resultat = new char[sizeof(char) + size1 + strlen(elem2) + 2];
    
   strcpy(resultat, elem1);
   
   //On rajoute le @
   resultat[size1] = '@';
   
   //Pour le strcat on rajoute le \0
   resultat[size1 + 1] = '\0'; 
   
   strcat(resultat, elem2);
   
   free(elem1);
   free(elem2);
   
   return resultat;
}

void Paire::converPaire(char * inChaine)
{
   if(inChaine)
   {
      char * a = new char[100];
      char * b = new char[100];
      int i = 0;
      int indTmp = 0;
      while(inChaine[i++] != '@');
      --i;
      
      strncpy(a, inChaine, i);
      

      a[i] = '\0';
      i++;


      while(inChaine[i] && inChaine[i] != '|')
      {
         b[indTmp] = inChaine[i];
         indTmp++;
         i++;
      }
      b[indTmp] = '\0';
      
      _paire[0] = a;
      _paire[1] = b;
      
      delete [] a;
      delete [] b;
   }
   
}





